/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package klijent;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import klijent.gui.RadSaVezbacima;
import server.util.Transfer;

/**
 *
 * @author Ivan Ivic
 */
public class Klijent {
    private static Klijent klijent;
    private Socket socket;
    private ObjectOutputStream out;
    private ObjectInputStream in;
    private Transfer transfer;
    private int signal;

    private Klijent()
    {
        try {
            socket = new Socket("127.0.0.1", 9000);
            out = new ObjectOutputStream(socket.getOutputStream());
            in = new ObjectInputStream(socket.getInputStream());
        } catch (UnknownHostException ex) {
            Logger.getLogger(Klijent.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Klijent.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static Klijent getInstance()
    {
        if (klijent == null)
            klijent = new Klijent();
        return klijent;
    }

    public static void postaviLookAndFeel()
    {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(RadSaVezbacima.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(RadSaVezbacima.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(RadSaVezbacima.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(RadSaVezbacima.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void posaljiObjekat(Object objekat, int brojOperacije)
    {
        transfer = new Transfer();
        transfer.setObjekat(objekat);
        transfer.setBrojOperacije(brojOperacije);
        posaljiTransfer(transfer);
    }

    private void posaljiTransfer(Transfer transfer)
    {
        try {
            out.writeObject(transfer);
        } catch (IOException ex) {
            Logger.getLogger(Klijent.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Object primiObjekat()
    {
        Object objekat = null;
        try {
            transfer = (Transfer) in.readObject();
            objekat = transfer.getObjekat();
            signal = transfer.getSignal();
        } catch (IOException ex) {
            Logger.getLogger(Klijent.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Klijent.class.getName()).log(Level.SEVERE, null, ex);
        }
        return objekat;
    }

    public int getSignal() {
        return signal;
    }

    public void setSignal(int signal) {
        this.signal = signal;
    }
    
}
